from flask import jsonify
from API import OperationMysql, request, app
import time
import json
# 自定义的数据库类
op_mysql = OperationMysql()

# 查询所有未完成任务
# post
@app.route('/task/index', methods=['post'])
def get_json():
    if request.method == 'POST':
        res = op_mysql.search_one("SELECT * FROM task  WHERE state = '1' order by id desc")
        return jsonify(res)

# 添加任务
# post
@app.route('/task/add', methods=['post'])
def add_task():
    if request.method == 'POST':
        formData = 'multipart/form-data'
        isFormData = formData in request.headers['Content-Type']
        content = ''
        if isFormData:
            content = request.form['content']
        else:
            content = json.loads(request.data.decode("utf-8")).get("content")
        t = time.time()
        t = int(t)
        print(t)
        res = op_mysql.insert_one("INSERT INTO task (content, creation_time, state, user_id, id, update_time) VALUES ('%s','%s',1,1,0,'%s')" % (content, t, t))
        return jsonify(res)

# # GET
# @app.route('/user', methods=['GET'])
# def get_user():
#     return jsonify({'user1': 1})

def Task():
    print("\033[1;32m执行任务APi\033[0m")